package com.example.blog.dao;

import com.example.blog.model.ArticleInfo;
import org.apache.ibatis.annotations.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * @Author YJ
 * @Date 2023/9/5 15:36
 * Description:文章表sql操作
 */
@Mapper
public interface ArticleMapper {

    @Select("select * from blog where userId=#{userId} order by userId desc,createTime desc")
    List<ArticleInfo> getListById(@Param("userId") int userId);

    @Delete("delete from blog where blogId=#{blogId} and userId=#{userId}")
    int del(@Param("blogId") int blogId, @Param("userId") int userId);

    @Insert("insert into blog(title,content,userId) values(#{title},#{content},#{userId})")
    int add(ArticleInfo articleInfo);

    @Select("select * from blog where blogId=#{blogId} and userId=#{userId} ;")
    ArticleInfo getArticleByIdAndBlogId(@Param("blogId") int blogId, @Param("userId") int userId);

    @Update("update blog set title=#{title},content=#{content},updateTime=#{updateTime} where blogId=#{blogId} and userId=#{userId}")
    int updateByUserId(@Param("blogId") int blogId, @Param("title") String title,
                       @Param("content") String content, @Param("userId") int userId,
                       @Param("updateTime") Timestamp updateTime);

    @Select("select * from blog where blogId=#{blogId}")
    ArticleInfo getDetailByBlogId(@Param("blogId") int blogId);

    @Select("select count(*) from blog where userId=#{userId}")
    int getReadCountByUserId(@Param("userId") int userId);

    @Update("update blog set rCount=rCount+1 where blogId=#{blogId}")
    int incrementReadCount(@Param("blogId") int blogId);

    @Select("select * from blog order by userId desc,createTime desc limit #{pSize} offset #{offset}")
    public List<ArticleInfo> getListByPage(@Param("pSize") int pSize,@Param("offset") int offset);

    @Select("select count(*) from blog")
    int getCount();
}